//
//  QLVisitorTableViewController.swift
//  WeiBo
//
//  Created by 黄跃奇 on 16/9/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
                                                           //MARK: - 5. 遵守协议
class QLVisitorTableViewController: UITableViewController, QLVisitorViewDelegate {

    //是否登录的标识
    var isLogin: Bool = false
    
    //全局的访客视图
    var visitorView: QLVisitorView?
    
    
    override func loadView() {
        
        if isLogin {
            super.loadView()
        } else {
            
            //自己提供一个视图
            visitorView = QLVisitorView()
            
            //MARK: - 4. 设置代理
            visitorView?.delegate = self
            //MARK: - 3.(闭包)设置点击登录和注册按钮的闭包
            visitorView?.loginClosure = { [weak self] in
                
                print("我的访客视图闭包调用过来的")
                
                self?.requestLoginOperation()
                
            }
            
            view = visitorView
            
            //未登录情况下添加导航栏按钮
            setupNavUI()
        }
    }
    
    //设置导航栏按钮
    private func setupNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(loginAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(loginAction))
    }
    
    //点击事件
    @objc func loginAction() {
        print("点击了登录操作")
        requestLoginOperation()
    }
    
    //MARK: - 6. 实现代理方法
    func didSelectedLogin() {
        print("我是访客视图代理对象调用过来的")
    }
    
    //请求登录的操作
    private func requestLoginOperation() {
        let OAuthVC = QLOAuthViewController()
        let nav = QLNavigationViewController(rootViewController: OAuthVC)
        present(nav, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        
        
    }

    

}
